﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace BDM
{
    public partial class PoortForm : Form
    {
        #region Variables
        private string _id;
        private Database _db;
        Message msg = new Message();
        bool bSpits = false;
        #endregion

        public PoortForm(string id, string location)
        {
            InitializeComponent();
            _id = id;
            _db = new Database();
            startPoort();
            positionPort(location);
        }

        public void positionPort(string location)
        {
            string[] pos = location.Split(';');
            int posX = Convert.ToInt32(pos[0].ToString());
            int posY = Convert.ToInt32(pos[1].ToString());
            this.Location = new Point(posX, posY);
        }

        private void startPoort()
        {
            DataTable dtData = _db.GetQuery("SELECT naam FROM locaties WHERE id = "+_id);

            string id = _id;
            if (id.Length < 2)
            {
                id = "0" + id;
            }

            Text = id + " - " + GetData.GetPortname(_id);

            runBGWorker();
        }

        private void runBGWorker()
        {
            if (!bgwReceiver.IsBusy)
            {
                bgwReceiver.RunWorkerAsync();
            }
            else
            {
                Thread.Sleep(10);
                runBGWorker();
            }
        }

        private void bgwReceiver_DoWork(object sender, DoWorkEventArgs e)
        {
            string id = _id;
            if (id.Length < 2)
            {
                id = "0" + id;
            }
            string sText = msg.Receive(id);
            lblReceive.Text = sText;
            if (sText[2].ToString() == "a")
            {
                setTransaction(sText);
            }
            else if (sText[2].ToString() == "b")
            {
                if (sText[3].ToString() == "n")
                {
                    bSpits = false;
                }
                else if (sText[3].ToString() == "s")
                {
                    bSpits = true;
                }
            }
            lblReceive.Text = "";
        }

        public void moveCar(string Costs)
        {
            pbCar.Location = new Point(-71, 38);
            lblCosts.Location = new Point(-51, 56);
            string sCosts = "€ ";
            for (int i = 0; i < Costs.Length; i++)
            {
                if (i == Costs.Length - 2)
                {
                    sCosts += ",";
                }
                sCosts += Costs[i].ToString();
            }
            lblCosts.Text = sCosts;
            for (int i = 0; i < 193; i++)
			{
                pbCar.Location = new Point(pbCar.Location.X + 1, pbCar.Location.Y);
                lblCosts.Location = new Point(lblCosts.Location.X + 1, lblCosts.Location.Y);
                Thread.Sleep(10);
			}
            lblCosts.Text = "";
            pbCar.Location = new Point(-71, 38);
            lblCosts.Location = new Point(-51, 56);
        }

        public void setTransaction(string Text)
        {
            moveCar(GetData.GetCarCaption(bSpits, Text[8].ToString() + Text[9].ToString(), Text[0].ToString() + Text[1].ToString(), Text[7].ToString()));
            if (GetData.GetPreparedMessageString(bSpits, Text) != null)
            {
                msg.Send(GetData.GetPreparedMessageString(bSpits, Text), "00");
            }
        }

        private void bgwReceiver_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            runBGWorker();
        }
    }
}
